package com.zxy.ziems.server.log.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.btp.common.response.PageResult;
import com.zxy.ziems.server.log.convert.OperLogConvert;
import com.zxy.ziems.server.log.mapper.OperLogMapper;
import com.zxy.ziems.server.log.pojo.entity.OperLogEntity;
import com.zxy.ziems.server.log.pojo.param.OperLogParam;
import com.zxy.ziems.server.log.pojo.vo.OperLogVO;
import com.zxy.ziems.server.log.service.OperLogService;
import com.zxy.ziems.server.mybatis.core.service.ServiceImplX;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class OperLogServiceImpl extends ServiceImplX<OperLogMapper, OperLogEntity> implements OperLogService {

    @Async
    @Transactional
    @Override
    public void insertOperlog(OperLogEntity operLog) {
        log.debug("insert log = {}", JSON.toJSONString(operLog));
        save(operLog);
    }

    @Override
    public PageResult<OperLogVO> page(OperLogParam param) {
        QueryWrapper<OperLogEntity> query = new QueryWrapper<>();
        PageResult<OperLogEntity> page = page(param, query);
        List<OperLogVO> collect = page.getList().stream().map(m -> OperLogConvert.INSTANCE.entity2Vo(m)).collect(Collectors.toList());
        return new PageResult<>(page.getTotal(), collect);
    }
}
